package cn.gz3c.uitest.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.huawei.hms.audioeditor.sdk.HAETimeLine;
import com.huawei.hms.audioeditor.sdk.HuaweiAudioEditor;
import com.huawei.hms.audioeditor.sdk.asset.HAEAsset;
import com.huawei.hms.audioeditor.ui.common.utils.i;

import java.util.List;

import cn.gz3c.uitest.R;
import cn.gz3c.uitest.bean.MenuItemBean;


public class MenuViewModel extends AndroidViewModel {
    public MutableLiveData<List<MenuItemBean>> ldListMenuItemBean = new MutableLiveData<>();
    public MutableLiveData<MenuHelper.MenuType> ldMenuType = new MutableLiveData<>();
    private MenuHelper menuHelper;
    private EditPreviewViewModel editPreviewViewModel;
    private ClipViewModel clipViewModel;
    private HuaweiAudioEditor huaweiAudioEditor;
    private HAETimeLine haeTimeLine;

    public MenuViewModel(@NonNull Application application) {
        super(application);
        this.a(application);
    }

    static {
        MenuViewModel.class.getSimpleName();
    }

    public void a(Application application) {
        if (this.menuHelper == null) {
            this.menuHelper = new MenuHelper(application.getApplicationContext());
        }

        this.ldMenuType.postValue(MenuHelper.MenuType.a);
    }

    public void a(MenuHelper.MenuType menuType) {
        this.ldListMenuItemBean.setValue(this.menuHelper.getListMenuItemBean(menuType));
    }

    public void a() {
        this.menuHelper.a();
    }

    public void a(EditPreviewViewModel editPreviewViewModel) {
        this.editPreviewViewModel = editPreviewViewModel;
        this.huaweiAudioEditor = editPreviewViewModel.getHuaweiAudioEditor();
        this.haeTimeLine = this.huaweiAudioEditor.getTimeLine();
    }

    public void a(ClipViewModel var1) {
        this.clipViewModel = var1;
    }

    public void d() {
        if (this.huaweiAudioEditor != null && this.haeTimeLine != null) {
            this.editPreviewViewModel.J();
            this.editPreviewViewModel.L();
            this.editPreviewViewModel.a(true);
            this.clipViewModel.b(this.haeTimeLine.getCurrentTime());
            this.huaweiAudioEditor.seekTimeLine(this.haeTimeLine.getCurrentTime(), this::b);
        }
    }

    public void c() {
        long var1 = 0L;
        HAEAsset var3 = this.editPreviewViewModel.w();
        if (this.haeTimeLine != null && var3 != null) {
            if (var3.getType() == HAEAsset.HAEAssetType.AUDIO) {
                if (var3.getStartTime() != this.haeTimeLine.getCurrentTime() && var3.getEndTime() != this.haeTimeLine.getCurrentTime()) {
                    if (this.haeTimeLine.getCurrentTime() - var3.getStartTime() >= 100L && var3.getEndTime() - this.haeTimeLine.getCurrentTime() >= 100L) {
                        if (this.haeTimeLine.getCurrentTime() > var3.getStartTime() && this.haeTimeLine.getCurrentTime() < var3.getEndTime()) {
                            var1 = this.haeTimeLine.getCurrentTime() - var3.getStartTime();
                        }

                        if (var1 != 0L) {
                            if (this.haeTimeLine.getAudioLane(var3.getLaneIndex()).splitAsset(var3.getIndex(), var1)) {
                                this.b();
                            }

                        }
                    } else {
                        i.a(this.getApplication(), this.getApplication().getString(R.string.no_split), 0).a();
                    }
                }
            }
        }
    }

    public void b() {
        this.editPreviewViewModel.J();
    }
}
